<?php

namespace App\Http\Controllers\Api;

use App\Models\Fang;
use App\Models\Fangattr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FangController extends Controller
{

    //房源列表
    public function fang(){
        //要返回的字段
        $field = [
            'id',
            'fang_name',
            'fang_pic',
            'fang_shi',
            'fang_ting',
            'fang_rent',
            'fang_build_area'
        ];
        //分页查询
        $data = Fang::select($field)->paginate(1);
        return $data;
    }
    //房源详情
    public function detail(Fang $fang){
        //获取房东数据
        $fang->owner;
        //房屋朝向
        $configArr = explode(',',$fang->fang_config);
        $fang->config = Fangattr::whereIn('id',$configArr)->get(['id','name','icon']);
        //房屋朝向
        $fang->direction = Fangattr::where('id',$fang->fang_direction)->value('name');
        //房屋图片
        $fang->piclist = array_map(function($item){
            return config('url.domain').$item;
        },explode('#',$fang->fang_pic));
        return $fang;
    }
    //推荐房源
    public function recommend(Request $request){
        $field =[
            'id',
            'fang_name',
            'fang_pic',
            'fang_shi',
            'fang_ting'
        ];
        $data = Fang::where('is_recommend','1')->orderBy('id','desc')->limit(5)->get($field);
        return $data;
    }
    //房源属性
    public function attr(Request $request){
        //指定要获取的属性字段
        $field_name =$request->get('field');
        //字段的id号 父级id
        $id = Fangattr::where('field_name',$field_name)->value('id');
        //获取数据
        return Fangattr::where('pid',$id)->limit(4)->get(['id','name','icon']);
    }
}
